package com.daocao.user.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.daocao.common.entity.UmsMenu;
import com.daocao.common.entity.dto.UmsMenuParamDto;
import com.daocao.common.entity.vo.RouterVO;
import com.daocao.common.response.DaoCaoPageResult;
import com.daocao.common.response.DaoCaoResult;
import com.daocao.common.service.UmsMenuService;
import io.netty.util.internal.ObjectUtil;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("sys/menu")
public class MenuController {

    private final UmsMenuService menuService;


    public MenuController(UmsMenuService menuService) {
        this.menuService = menuService;
    }

    @PostMapping
    public DaoCaoResult savaMenu(@Valid @RequestBody UmsMenu umsMenu) {
        int i = menuService.saveMenu(umsMenu);
        if (i > 0) {
            return DaoCaoResult.success();
        }
        return DaoCaoResult.error();
    }

    @GetMapping("self")
    public DaoCaoResult searchSelfMenu() {
        List<RouterVO> routerVOList = menuService.searchSelfMenu();
        return DaoCaoResult.success(routerVOList);
    }

    @GetMapping("list")
    public DaoCaoResult list(UmsMenuParamDto menuParamDto) {
        DaoCaoPageResult<UmsMenu> pageResult = menuService.selectList(menuParamDto);
        return DaoCaoResult.success(pageResult);
    }

    @GetMapping("{id}")
    public DaoCaoResult searchInfo(@PathVariable("id") Long id) {
        UmsMenu menu = menuService.searchInfo(id);
        return DaoCaoResult.success(menu);
    }

    @PutMapping
    public DaoCaoResult updateMenu(@Valid @RequestBody UmsMenu umsMenu) {
        int row = menuService.updateMenu(umsMenu);
        if (row > 0) {
            return DaoCaoResult.success();
        }
        return DaoCaoResult.error("修改菜单失败");
    }

    @DeleteMapping
    public DaoCaoResult removeMenu(@RequestBody Long[] ids) {
        int row = menuService.removeMenu(ids);
        if (row > 0) {
            return DaoCaoResult.success();
        }
        return DaoCaoResult.error();
    }
}
